var config = require('config');
cc.Class({
    extends: cc.Component,

    properties: {
        _acc: cc.p(0, 0),
        yaoAudio: {
            default: null,
            url: cc.AudioClip
        },
        wardsAudio: {
            default: null,
            url: cc.AudioClip
        },
        countlabel:{
            default:null,
            type: cc.Label
        },
        times: 0,
        countDown: 10,
        startMove: false,
        confirm: {
            default: null,
            type: cc.Node
        }
        // foo: {
        //    default: null,      // The default value will be used only when the component attaching
        //                           to a node for the first time
        //    url: cc.Texture2D,  // optional, default is typeof default
        //    serializable: true, // optional, default is true
        //    visible: true,      // optional, default is true
        //    displayName: 'Foo', // optional
        //    readonly: false,    // optional, default is false
        // },
        // ...
    },

    // use this for initialization
    onLoad: function () {
        cc.director.preloadScene('wards', function () {
            cc.log('wards scene preload');
        });
        this.confirm.getComponent('confirm').boom = this;
        cc.inputManager.setAccelerometerEnabled(true);
        cc.systemEvent.on(cc.SystemEvent.EventType.DEVICEMOTION, this.onDeviceMotionEvent, this);
    },
    
    destroy () {
        cc.inputManager.setAccelerometerEnabled(false);
        cc.systemEvent.off(cc.SystemEvent.EventType.DEVICEMOTION, this.onDeviceMotionEvent, this);
    },
    
    onDeviceMotionEvent (event) {
        this._acc.x =  event.acc.x;
        this._acc.y =  event.acc.y;
        var roteLeft = cc.rotateTo(0.1, -10);
        var roteO = cc.rotateTo(0.1, 0);
        var roteRight = cc.rotateTo(0.15, 10);
        var action = cc.sequence(roteLeft, roteO,roteRight,roteO);
        if(this.startMove){
            if(this._acc.x > 0.5 || this._acc.y > 0.5){
                cc.audioEngine.playEffect(this.yaoAudio, false);
                this.times++;
                this.node.runAction(action);
                if(this.times >3){
                    this.schedule(function(){
                        this.getWards();
                }, 1);
            }
        }
    }
    },
    
    getWards:function() {
        cc.log('touch end');
        cc.audioEngine.playEffect(this.wardsAudio, false);
        cc.director.loadScene('wards');
        
    },
    
    startGame: function() {
        this.schedule(this.startDown,1);
    },
    
    startDown: function() {
         this.countlabel.string = --this.countDown;
    },
    
    update: function(dt){
        if(this.countDown <= 0){
            this.unschedule(this.startDown);
            this.getWards();
        }
    }
    
    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
});
